import {
    banner,
    pay,
    getCardInfo,
    getUserInfo, getOpenCode
} from '../../utils/apis'

const {$Toast} = require('../../dist/base/index');

var app = getApp();

Page({
    data: {
        hasRegister: false,
        registerClass: '.card-deactive',
        endTime: '',
        invalid: true,
        current: 'homepage'
    },
    onShow: function () {

    },
    onLoad: function (e) {
        this.getLoginInfo(this.setLoginInfo);
    },
    register: function () {
        wx.navigateTo({
            url: '/pages/register/register'
        })
    },
    onGotUserInfo({detail: {userInfo}}) {
        app.globalData.wxUserInfo = userInfo;
        wx.setStorageSync('wxUserInfo', userInfo);
        if (userInfo) {
            this.register();
        } else {
            $Toast({
                content: '您需要授权才可注册哦',
                type: 'error'
            });
        }
    },
    renew() {
        let that = this;
        wx.login({
            success(res) {
                if (res.code) {
                    getOpenCode({
                        code: res.code,
                        success: (response) => {
                            let openCode = JSON.parse(response).openid;
                            pay({
                                params: {
                                    total_fee: 4.9,
                                    uid: app.globalData.loginInfo.id,
                                    id: app.globalData.cardInfo.id,
                                    openid: openCode
                                },
                                success:function(res){
                                    $Toast({
                                        content: '付款成功',
                                        type: 'success'
                                    });
                                },
                                fail: function (res) {
                                    $Toast({
                                        content: '付款失败',
                                        type: 'error'
                                    });
                                },
                                complete:function (res) {
                                    that.onLoad();
                                }
                            })
                        }
                    })
                } else {
                    $Toast({
                        content: '网络错误',
                        type: 'warning'
                    });
                }
            }
        });

    },
    onShareAppMessage: function () {
    },

    getCardInfo() {
        var that = this;
        getCardInfo({
            userId: app.globalData.loginInfo.id,
            success: (response) => {
                app.globalData.cardInfo = {
                    startTime: (new Date(response.card.start)).getTime(),
                    endTime: (new Date(response.card.end)).getTime(),
                    id: response.card.id
                };
                let invaildFlag = false;
                let classFlag = '.card-active';
                if ((new Date()).getTime() > app.globalData.cardInfo.endTime) {
                    invaildFlag = true;
                    classFlag = '.card-deactive';
                }
                that.setData({
                    'endTime': response.card.end,
                    'registerClass': classFlag,
                    'invalid': invaildFlag
                });
            }
        })
    },
    getLoginInfo: function (save) {
        //先从缓存中取  这里背注释了 因为在付款完毕以后应该刷新页面  而onload方法只会在加载页面的时候调用  所以可以不用判断
        // let info = app.globalData.loginInfo;
        // if (info && JSON.stringify(info) !== '{}') {
        //     return;
        // }
        // 缓存不存在 登录操作
        wx.login({
            success(res) {
                if (res.code) {
                    getUserInfo({
                        code: res.code,
                        success: (response) => {
                            save(response);
                        }
                    })
                } else {
                    $Toast({
                        content: '网络错误',
                        type: 'warning'
                    });
                }
            }
        });
    },
    setLoginInfo(loginInfo) {
        if (!loginInfo || JSON.stringify(loginInfo) === '{}') {
            this.setData({
                'hasRegister': false,
            });
        } else {
            this.setData({
                'hasRegister': true
            });
            app.globalData.loginInfo = loginInfo;
            this.getCardInfo();
        }
    },

});